<?php $this->load->view('common/head');?>
<?php $this->load->view('common/navbar');?>
<style type="text/css">
	.storeList{overflow:hidden;}
	.storeList li{ width: 150px; float: left;  }
</style>
<form class="form-horizontal margin-top-10" name="add" id="add" action="<?php echo $this->input->server('REQUEST_URI'); ?>" method="post">
	<input type="hidden" name="procId" id="procId" value="<?=$procData['id']?>">
	<div class="panel panel-default">
	  <!-- Default panel contents -->
	  <div class="panel-heading">请选择协办店及填写投放费用</div>

	  <!-- Table -->
	  <div class="panel-body" style="border:1px solid #cccccc; padding: 20px;">
	  	<ul class="storeList">
	  		<?php foreach ($storeList as $storeId => $storeData): ?>
	  		<li>
	  			<input id="store_<?=$storeId?>" name="store" type="checkbox" <?=$storeData['isChecked'] ? 'checked':''?> <?=$procData['storeId'] == $storeData['storeId'] ? 'onclick="return false;"' : ''?> storeType="<?=$procData['storeId'] == $storeData['storeId'] ? '申报店' : '协办店'?>" storeId="<?=$storeId?>" storeName="<?=$storeData['storeName']?>" coding="<?=$storeData['coding']?>" applyMoney="<?=$storeData['applyMoney']?>" yearMoney="<?=$storeData['yearMoney']?>" value="<?=$storeId?>">
	  			<label for="store_<?=$storeId?>"><?=$storeData['storeName']?></label>
	  		</li>
	  		<?php endforeach ?>
	  	</ul>
	  </div>
	</div>
	<table class="table table-condensed table-bordered mb27">
		<thead class="text-center">
	      <tr>
	        <td class="bg-primary text-center" width="20%">类型</td>
	        <td class="bg-primary text-center" width="20%">专营店名称</td>
	        <td class="bg-primary text-center" width="30%">年度额度</td>
	        <td class="bg-primary text-center" width="30%">申报金额</td>
	      </tr>
	    </thead>
		<tbody class="text-center" id="inputStoreList">
			<tr>
				<td>投放类型</td>
				<td>专营店</td>
				<td>555<input type="hidden" name="storeList[][applyMoney]" id="storeList[][applyMoney]" value=""></td>
				<td>
					<input type="number" required min="0" step="0.01" class="form-control" name="applyMoney" id="applyMoney" value="" placeholder="申报金额">
				</td>
			</tr>
		</tbody>
		<tbody class="text-center">
			<tr>
				<td colspan="2">总计:</td>
				<td><span class="yearTotalMoney">0</span></td>
				<td><span class="applyTotalMoney">0</span></td>
			</tr>
		</tbody>
	</table>

	<div class="panel panel-default">
	  <!-- Default panel contents -->
	  <div class="panel-heading">请填写投放类型</div>
	  <div style="text-align: right;overflow: hidden;">
	  	<a class="btn btn-default col-md-offset-10" onclick="newWindow('新增','<?=printUrl('SpreadBudget','storeProjectAdd', array('procId'=>$procData['id']))?>',800,550,'reloadData')">添加</a>
	  </div>
	  <!-- Table -->
	  <div id="storeProjectList" class="panel-body" style="margin-top: 10px;clear: both;">
		  	
	  </div>
	</div>
	<table class="table table-condensed">
	    <tbody>
	      <tr>
	        <td class="bg-primary col-md-1">申报金额：</td>
	        <td><span class="applyTotalMoney">0</span></td>
	      </tr>
	      <tr>
	        <td class="bg-primary col-md-1">合同金额：</td>
	        <td><span class="projectApplyMoney">0</span></td>
	      </tr>
	    </tbody>
	  </table>
	  <div class="text-center" style="margin-top: 10px;">
	    <button type="submit" class="btn btn-default btn-primary">提交</button>
		<button class="btn btn-default btn-primary" type="button" onclick="window.location.href='<?=$breadcrumb[count($breadcrumb)-2]['url']?>'">返回</button>

		</div>
</form>
<script type="text/javascript" src="public/js/commonSpread.js"></script>
<script type="text/javascript">
$(function(){
  initValidation();
  newWindowReload();
  initStoreList();
  $("input:checkbox[name='store']").change(function(){
	initStoreList();
	initValidation();
  });
  $(document).on('change','.storeInputApplyMoney',function(){
  	$("#store_"+$(this).attr('storeId')).attr("applyMoney", $(this).val());
  	countApplyTotalMoney();
  });
});
//初始化验证
function initValidation(){
	$('form[name="add"]').find("input,select,textarea").jqBootstrapValidation("destroy");
	$('form[name="add"]').find("input,select,textarea").not("[type=submit]").jqBootstrapValidation({
	    preventSubmit:false,
	    submitSuccess: function (form, event) {
	         event.preventDefault();
	         ajaxFormSubmit('add');
	     },
	     submitError: function (form, event, errors) {
	       event.preventDefault();
	       }
  	});
}
function newWindowReload(){
	$("#storeProjectList").load('<?=printUrl('SpreadBudget','storeProjectList', array('procId'=>$procData['id']))?>');
	$('.projectApplyMoney').load('<?=printUrl('SpreadBudget','getStoreProjectApplyMoney', array('procId'=>$procData['id']))?>');
}
function initStoreList(){
	var html = '';
	var yearTotalMoney = 0;
	var applyTotalMoney = 0;
	$("input:checkbox[name='store']:checked").each(function (index, domEle){
		var storeId = $(domEle).attr('storeId');
		var yearMoney = $(domEle).attr('yearMoney');
		var applyMoney = $(domEle).attr('applyMoney');
		var storeName = $(domEle).attr('storeName');
		html +='<tr>';
			html += '<td>'+$(domEle).attr('storeType')+'</td>';
			html += '<td><input type="hidden" name="storeList['+storeId+'][storeName]" id="storeList['+storeId+'][storeName]" value="'+storeName+'">'+storeName+'</td>';
			html += '<td><input type="hidden" name="storeList['+storeId+'][yearMoney]" id="storeList['+storeId+'][yearMoney]" value="'+yearMoney+'">'+yearMoney+'</td>';
			html += '<td>';
				html += '<input type="number" required min="0" step="0.01" class="form-control storeInputApplyMoney" name="storeList['+ storeId +'][applyMoney]" id="storeList['+ storeId +'][applyMoney]" storeId="'+ storeId +'" value="'+applyMoney+'" placeholder="申报金额">';
			html += '</td>';
		html += '</tr>';
		yearTotalMoney += parseFloat(yearMoney);
		applyTotalMoney += parseFloat(applyMoney);
	});
	$("#inputStoreList").html(html);
	$(".yearTotalMoney").html(yearTotalMoney);
	$(".applyTotalMoney").html(applyTotalMoney);
}
function countApplyTotalMoney(){
	var applyTotalMoney = 0;
	$('.storeInputApplyMoney').each(function (index, domEle){
		applyTotalMoney += parseFloat($(domEle).val());
	});
	$(".applyTotalMoney").html(applyTotalMoney);
}
</script>
<?php $this->load->view('common/foot');?>